from datetime import datetime, timedelta, date

from django import forms
from django.utils.timezone import make_aware

from ui.models import FileModel, OrderModel, SheetsModel, RouteFigsModel, GvkmUser


class FilesForm(forms.Form):
    order_file = forms.FileField(label='Выбор файла')

    def __init__(self, *args, **kwargs):
        super(FilesForm, self).__init__(*args, **kwargs)
        self.fields['order_file'].widget.attrs['style'] = 'width:40%;'


class OrdersForm(forms.Form):
    order_query = FileModel.objects.order_by('-datetime')
    order_files = forms.ModelChoiceField(
        queryset=order_query.filter(
            datetime__gte=make_aware(datetime.today() - timedelta(days=7))
        )
        , label='Выбор файла'
    )
    order_date = forms.DateField(
        initial=date.today,
        widget=forms.SelectDateWidget(
            empty_label=("Choose Year", "Choose Month", "Choose Day")
        ),
        label='Дата маршрутов'
    )
    city = forms.ChoiceField(
        choices=(('Spb', 'Санкт-Петербург'), ('Moscow', 'Москва')),
        label='Город маршрутизации'
    )
    lat = forms.DecimalField(
        max_digits=9, decimal_places=6,
        label='Широта начала маршрута',
        max_value=90.0, min_value=-90.0,
        widget=forms.TextInput({"placeholder": "От -90.0 до 90.0"}),
        required=False
    )
    lon = forms.DecimalField(
        max_digits=9, decimal_places=6,
        label='Долгота начала маршрута',
        max_value=180.0, min_value=-180.0,
        widget=forms.TextInput({"placeholder": "От -180.0 до 180.0"}),
        required=False
    )
    # vkm_count = forms.DecimalField(
    #     min_value=1, max_value=500, max_digits=3, decimal_places=0,
    #     label='Количество ВКМ'
    # )
    # soft = forms.BooleanField(required=False, label='Подбор кол-ва курьеров')
    # capacity = forms.DecimalField(
    #     min_value=0, max_value=100, max_digits=3, decimal_places=0, required=False,
    #     label='Кол-во доставок на курьера',
    #     widget=forms.TextInput({"placeholder": "7"})
    # )
    #
    # def clean(self):
    #     cleaned_data = super(OrdersForm, self).clean()
    #     vkm_count = cleaned_data.get('vkm_count')
    #     #lat = cleaned_data.get('lat')
    #     #lon = cleaned_data.get('lon')
    #     capacity = cleaned_data.get('capacity')
    #     if not capacity and not vkm_count:
    #         raise forms.ValidationError('You have to write something!')

    def __init__(self, *args, **kwargs):
        super(OrdersForm, self).__init__(*args, **kwargs)
        self.fields['order_files'].widget.attrs['style'] = 'width:35%;'
        self.fields['order_date'].widget.attrs['style'] = 'width:13%;'
        # self.fields['vkm_count'].widget.attrs['style'] = 'width:5%;'
        # self.fields['vkm_count'].widget.attrs['onclick'] = "javascript:toggleDiv('soft');"
        # self.fields['soft'].widget.attrs['id'] = 'soft'
        self.fields['city'].widget.attrs['style'] = 'width:13%;'
        # self.fields['capacity'].widget.attrs['style'] = 'width:5%'
        self.fields['lat'].widget.attrs['style'] = 'width:13%'
        self.fields['lon'].widget.attrs['style'] = 'width:13%'
        # self.fields['soft'].widget.attrs['style'] = 'width:3%'
        #self.fields['soft'].widget.attrs['style'] = 'float: left;'



class RoutesForm(forms.Form):
    route_query = OrderModel.objects.order_by('-datetime')
    route_files = forms.ModelChoiceField(queryset=route_query.filter(
        datetime__gte=make_aware(datetime.today() - timedelta(days=7))), label='Выбор файла')
    vkm_count = forms.DecimalField(
        min_value=1, max_value=500, max_digits=3, decimal_places=0,
        label='Количество ВКМ'
    )
    soft = forms.BooleanField(required=False, label='Подбор кол-ва курьеров')
    capacity = forms.DecimalField(
        min_value=0, max_value=100, max_digits=3, decimal_places=0, required=False,
        label='Кол-во доставок на курьера',
        widget=forms.TextInput({"placeholder": "7"})
    )

    def clean(self):
        cleaned_data = super(RoutesForm, self).clean()
        vkm_count = cleaned_data.get('vkm_count')
        # lat = cleaned_data.get('lat')
        # lon = cleaned_data.get('lon')
        capacity = cleaned_data.get('capacity')
        if not capacity and not vkm_count:
            raise forms.ValidationError('You have to write something!')

    def __init__(self, *args, **kwargs):
        super(RoutesForm, self).__init__(*args, **kwargs)
        self.fields['route_files'].widget.attrs['style'] = 'width:40%;'
        self.fields['vkm_count'].widget.attrs['style'] = 'width:5%;'
        self.fields['vkm_count'].widget.attrs['onclick'] = "javascript:toggleDiv('soft');"
        self.fields['soft'].widget.attrs['id'] = 'soft'
        self.fields['capacity'].widget.attrs['style'] = 'width:5%'
        # self.fields['lat'].widget.attrs['style'] = 'width:13%'
        # self.fields['lon'].widget.attrs['style'] = 'width:13%'
        self.fields['soft'].widget.attrs['style'] = 'width:3%'


class RouteFigsForm(forms.Form):
    figs_query = RouteFigsModel.objects.order_by('-datetime')
    figures = forms.ModelChoiceField(queryset=figs_query.filter(
        datetime__gte=make_aware(datetime.today() - timedelta(days=7))), label='Выбор изображения')

    def __init__(self, *args, **kwargs):
        super(RouteFigsForm, self).__init__(*args, **kwargs)
        self.fields['figures'].widget.attrs['style'] = 'width:40%;'


class SheetsForm(forms.Form):
    sheet_query = SheetsModel.objects.order_by('-datetime')
    sheet_files = forms.ModelChoiceField(queryset=sheet_query.filter(
        datetime__gte=make_aware(datetime.today() - timedelta(days=7))), label='Выбор файла')

    def __init__(self, *args, **kwargs):
        super(SheetsForm, self).__init__(*args, **kwargs)
        self.fields['sheet_files'].widget.attrs['style'] = 'width:40%;'


class GvkmUserForm(forms.ModelForm):
    class Meta:
        model = GvkmUser
        fields = "__all__"


